from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from goods.models import SKUImage, SKU
from meiduo_admin.serializer.images import ImageSerializer, SKUSerializer
from meiduo_admin.utils import PageNum

# 图片表的增删改查
class ImageView(ModelViewSet):

    # 权限指定
    permission_classes = [IsAdminUser]
    # 序列化器指定
    serializer_class = ImageSerializer
    # 查询集指定
    queryset = SKUImage.objects.all()
    # 分页器指定
    pagination_class = PageNum

    # 在保存图片之前 先获取sku数据
    # @action(methods=['get'],detail=False)
    def simple(self,request):
        # 获取sku数据
        skus = SKU.objects.all()
        # 序列化返回sku数据
        ser = SKUSerializer(skus,many=True)

        return Response(ser.data)
